<template>
    <div>
        <m_block-index-nav-bar title="查看任务"/>
        <van-nav-bar
                v-if="false"
                title="查看任务"
                left-text="返回"
                left-arrow
                @click-left="back"
                :fixed="true"
        />
        <form.vue :fields="fields" />
        <div class="btns">
            <van-button class="btn" type="info" @click="save(false)" v-if="source.deal">保存</van-button>
            <van-button class="btn" type="info" @click="save(true)" v-if="source.deal">提交</van-button>
        </div>
    </div>
</template>

<script>
    import Form from "./form"
    import M_blockIndexNavBar from "./m_blockIndexNavBar";
    import url from "../url/url";

    export default {
        name: "deal",
        components: {M_blockIndexNavBar, "form.vue": Form},
        data() {
            return {
                fields: [],
                source: {},
                id: "",
            }
        },
        methods: {
            back() {
                this.$router.back()
            },
            load(id) {
                this.id = id;
                var a1 = $.ajax({
                    type: "POST",
                    url: url.showBpmInfo,//"/api/bpm/workflow/getInsInfo",
                    data: JSON.stringify({id, args: {id}, nodeId: ""}),
                    contentType: "application/json"
                });
                var a2 = $.get(url.showBpmMobileFields /*"/api/bpm/workflow/getMobileFormByInsId"*/, {id})
                $.when(a1, a2).then((a, b) => {
                    var r = a[0];
                    if (r.success) {
                        var d = this.source = r.data;
                        this.fields = Object.values(r.data.allFields)
                        //排序
                        try {
                            var ms = b[0].data.mobileSetting[0].fields;
                            this.fields = this.fields.sort((a, b) => ms.indexOf(a.title) - ms.indexOf(b.title))
                        } catch (e) {
                        }
                        //值捕获
                        this.fields.forEach(e => {
                            e.value = d.attrs[e.title] || null
                            //我可以编辑的字段
                            e.editable = (d.deal && d.formFields.includes(e.title))
                            e.required = (d.requiredFields||[]).includes(e.title)
                        });

                    }
                });
            },


            /**
             * 保存任务
             * 只保存而不进行提交
             *
             * 参数为true的时候，进行提交
             */
            save(submit) {
                console.log(submit)
                console.log(this.source)
                var data = {
                    "data": {
                        ...(() => {
                            var mp = {}
                            this.fields
                                .filter(e => e.editable)
                                .forEach(e => {
                                    mp[e.title] = e.value
                                })
                            return mp
                        })()
                        // "测试文本框": "31312321",
                        // "测试多行文本": "321313",
                        // "测试下拉菜单": "2",
                        // "测试单选": "1",
                        // "测试复选": "2,3",
                        // "时间格式1": "2020-06-02 00:00:00",
                        // "时间格式2": "2020-06-30",
                        // "时间格式3": "08:02:02",
                        // "测试签名": ""
                    },
                    "files": [],
                    "sign": {
                        "idea": "同意",
                        "info": "",
                        "files": []
                    },
                    "id": this.id,//"5ef422d6735be5037235f230",
                    "mode": "deal",
                    "logId": this.source.logId,//"5ef422d660b24de9b2f9d52e",
                    "validate": !!submit
                }
                this.$toast.loading({
                    message: '提交中...',
                    forbidClick: true,
                })
                $.ajax({
                    type: 'POST',
                    url: url.doSaveBpm,//"/api/bpm/workflow/saveIns",
                    contentType: "application/json",
                    data: JSON.stringify(data)
                }).then(r => {
                    this.$toast.clear(true)
                    if (!r.success) {
                        this.$notify({type: 'danger', message: r.errMessage});
                    } else {
                        if (submit) {
                            this.$router.push({
                                path: "/next",
                                query: {
                                    id: this.id,
                                    logId: this.source.logId
                                }
                            })
                        } else {
                            this.$notify({type: 'primary', message: "保存成功"});
                        }
                    }
                }).fail(() => {
                    this.$toast.clear(true)
                });

            }
        },
        mounted() {
            var id = this.$route.query.id
            this.load(id)
        }
    }
</script>

<style scoped>
    .btns {
        /*position: fixed;*/
        width: 100%;
        text-align: center;
        padding-top: 20px;
        padding-bottom: 20px;
        /*bottom: 20px;*/
    }

    .btns .btn {
        width: 100px;
    }

    .btns button {
        height: 34px;
    }

    .btns .btn:last-child {
        margin-left: 20px;
    }
</style>
